package com.joker.airoleplayplatformback.controller;

import com.joker.airoleplayplatformback.domain.dto.VoiceInfo;
import com.joker.airoleplayplatformback.domain.entity.R;
import com.joker.airoleplayplatformback.domain.po.AiAgentPO;
import com.joker.airoleplayplatformback.service.AiAgentService;
import com.joker.airoleplayplatformback.utils.VoiceTypeListUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI智能体控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/agents")
@Tag(name = "AI智能体管理", description = "AI智能体相关接口")
public class AiAgentController {

    @Autowired
    private AiAgentService aiAgentService;

    @GetMapping("/list")
    @Operation(summary = "获取所有启用的智能体列表")
    public R<List<AiAgentPO>> getEnabledAgents() {
        try {
            List<AiAgentPO> agents = aiAgentService.getEnabledAgents();
            return R.ok(agents);
        } catch (Exception e) {
            log.error("获取智能体列表失败", e);
            return R.error(500, "获取智能体列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取智能体详情")
    public R<AiAgentPO> getAgentById(@PathVariable Long id) {
        try {
            AiAgentPO agent = aiAgentService.getAgentById(id);
            if (agent == null) {
                return R.error(500, "智能体不存在");
            }
            return R.ok(agent);
        } catch (Exception e) {
            log.error("获取智能体详情失败", e);
            return R.error(500, "获取智能体详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/key/{agentKey}")
    @Operation(summary = "根据agentKey获取智能体详情")
    public R<AiAgentPO> getAgentByKey(@PathVariable String agentKey) {
        try {
            AiAgentPO agent = aiAgentService.getAgentByKey(agentKey);
            if (agent == null) {
                return R.error(500, "智能体不存在");
            }
            return R.ok(agent);
        } catch (Exception e) {
            log.error("获取智能体详情失败", e);
            return R.error(500, "获取智能体详情失败: " + e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "创建智能体")
    public R<AiAgentPO> createAgent(@RequestBody AiAgentPO agent) {
        try {
            AiAgentPO createdAgent = aiAgentService.createAgent(agent);
            return R.ok(createdAgent);
        } catch (Exception e) {
            log.error("创建智能体失败", e);
            return R.error(500, "创建智能体失败: " + e.getMessage());
        }
    }

    @PutMapping
    @Operation(summary = "更新智能体")
    public R<AiAgentPO> updateAgent(@RequestBody AiAgentPO agent) {
        try {
            AiAgentPO updatedAgent = aiAgentService.updateAgent(agent);
            return R.ok(updatedAgent);
        } catch (Exception e) {
            log.error("更新智能体失败", e);
            return R.error(500, "更新智能体失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除智能体")
    public R<Boolean> deleteAgent(@PathVariable Long id) {
        try {
            boolean result = aiAgentService.deleteAgent(id);
            return R.ok(result);
        } catch (Exception e) {
            log.error("删除智能体失败", e);
            return R.error(500, "删除智能体失败: " + e.getMessage());
        }
    }


    @GetMapping("/voice-list")
    @Operation(summary = "获取音色列表")
    public R<List<VoiceInfo>> getVoiceList() {
        try {
            List<VoiceInfo> voiceList = aiAgentService.getVoiceTypeList();
            return R.ok(voiceList);
        } catch (Exception e) {
            log.error("获取音色列表失败", e);
            return R.error(500, "获取音色列表失败: " + e.getMessage());
        }
    }
}
